package com.virtualightning.mvvm.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.virtualightning.mvvm.R
import com.virtualightning.mvvm.extensions.forEach

/**
 *  Anchor : Create by CimZzz
 *  Time : 2021/12/27 14:44
 *
 */
class TopBarLayout: ViewGroup {
	constructor(context: Context?) : super(context)
	constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
	constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
		context,
		attrs,
		defStyleAttr
	)


	override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec)

		var remainedHeight = measuredHeight

		var contentHeight = 0
		forEach { child ->
			if (child.visibility != View.GONE) {
				val lp = child.layoutParams as NestedLayout.LayoutParams
				measureChild(child, widthMeasureSpec, heightMeasureSpec)
				if (lp.behaviorFlag == NestedLayout.LayoutParams.BehaviorFlag_Fix) {
					remainedHeight -= child.measuredHeight
				}
				contentHeight += child.measuredHeight + lp.topMargin + lp.bottomMargin
			}
		}
	}

	override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
		var top = t
		forEach { child ->
			val lp = child.layoutParams as NestedLayout.LayoutParams
			val realTop = top + lp.topMargin
			val realBottom = realTop + child.measuredHeight
			lp.scrollTopBound = realTop.toFloat()
			lp.scrollBottomBound = realBottom.toFloat()
			child.layout(l, realTop, r, realBottom)
			top = realBottom + lp.bottomMargin
		}
	}

	override fun generateLayoutParams(attrs: AttributeSet): ViewGroup.LayoutParams {
		return LayoutParams(context, attrs)
	}

	override fun generateLayoutParams(p: ViewGroup.LayoutParams): ViewGroup.LayoutParams {
		return LayoutParams(p.width, p.height)
	}

	override fun generateDefaultLayoutParams(): ViewGroup.LayoutParams {
		return LayoutParams(
			ViewGroup.LayoutParams.WRAP_CONTENT,
			ViewGroup.LayoutParams.WRAP_CONTENT
		)
	}



	///////////////////////////////////////////////////////////////////////////
	// 代码分割线 on 2021/12/16 2:22 下午 by CimZzz
	// 以下内容为:
	// 内部类
	///////////////////////////////////////////////////////////////////////////

	/**
	 * 布局参数
	 */
	class LayoutParams : MarginLayoutParams {
		companion object {
			/**
			 * 允许滑动
			 */
			const val ScrollFlag_Scroll = 0

			/**
			 * 固定
			 */
			const val ScrollFlag_Fix = 1
		}

		constructor(width: Int, height: Int) : super(width, height)
		constructor(c: Context, attrs: AttributeSet) : super(c, attrs) {
			val typedArray = c.obtainStyledAttributes(attrs, R.styleable.TopBarLayout_Layout)
			scrollFlags =
				typedArray.getInt(R.styleable.TopBarLayout_Layout_scrollFlags, scrollFlags)
			typedArray.recycle()
		}

		/**
		 * 行为类型，默认为滑动
		 */
		internal var scrollFlags: Int = ScrollFlag_Scroll

		/**
		 * 顶部滑动边界
		 */
		internal var scrollTopBound: Float = 0f

		/**
		 * 底部滑动边界
		 */
		internal var scrollBottomBound: Float = 0f
	}
}